layui.use(['form','layer','jquery','laypage','table'],function(){
    var layer=layui.layer,table=layui.table,form=layui.form,$=layui.jquery;

    //方法级渲染
    var tabopt={
        id: 'iotconfig',
        elem: '#iotList'
        ,width:'100%'
       /* ,loading: true*/
        ,cellMinWidth: '80'
        , url: '/iot/manager/list'
        ,defaultToolbar: ['exports','filter','print']
        ,toolbar:'#toolbarDemo'
        , cols: [[
            {type:'checkbox', fixed:'left'}
            , {field: 'deviceName', title: '所属设备',width:'160'}
            , {field: 'port',title: '端口',width:'160',sort:true}
            , {field: 'boss', title: '监听线程数',width:'160'}
            , {field: 'work', title: 'io事件处理线程数',width:'160'}
            /*, {field: 'queueLength',title:'请求队列大小',width:'160'}*/
          /*  , {field: 'decoder',title:'解码器类名称',width:'180',event:'decoder'}
            , {field: 'encoder',title:'编码器类名称',width:'180',event:'encoder'}*/
            , {field: 'handler',title:'业务处理类名称',width:'180',event:'handler'}
            , {field: 'allIdleTime',title:'心跳时间',width:'160'}
            , {field: 'isOpen',title:'是否开启',width:'160',templet:function(d){
                    if (d.isOpen =='1'){
                        return '已开启';
                    }else {
                        return '已关闭';
                    }
                }}
            , {field: 'right', title: '操作',toolbar: "#barDemo",fixed:'right',width:'300'}
        ]]
        , page: true
        , request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusCode: 200 //成功的状态码，默认：0
           , countName: 'count' //数据总数的字段名称，默认：count
            , dataName: 'data' //数据列表的字段名称，默认：data
        }
        ,even: true
    };
    table.render(tabopt);

    /*
   * 监听行事件
   * */
    table.on('tool(iotconfig)',function (obj) {
        var data=obj.data;
        console.log(obj);
        tableoperation[obj.event](data);
    });

    //监听头事件
    table.on('toolbar(iotconfig)', function(obj){
        console.log(obj);
        var checkStatus = table.checkStatus(obj.config.id);
        console.log(checkStatus);
        var data = checkStatus.data;
        console.log(data);
        switch(obj.event){
            case 'delbatch':
                if (data.length<1){
                    layer.msg("请选择一行！！", {shift: 6, icon: 2});
                    return;
                }
                var arr=[];
                data.forEach(function (item,index) {
                    arr.push(item.port);
                });
                console.log(arr);
                tableoperation[obj.event](arr.join(","));
                break;
            case 'iot_add':
                layer.open({
                    title : "添加端口",
                    area: ['90%', '90%'],
                    offset: 't',
                    fix: false,
                    maxmin: true,
                    shadeClose: false,
                    shade: 0.4,
                    type : 2,
                    content : "/iot/manager/addIotServer"
                    ,cancel:function (index,layero) {
                        /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                        table.reload("iotconfig");
                    }
                });
                break;
            case 'add_clz':
                openLayer("");
                break;

        };
    });

    var tableoperation={
         handler: function (data) {
             openLayer(data['handler'])
         }
        ,encoder: function(data){
            openLayer(data.encoder)
        }
        ,decoder: function (data) {
            openLayer(data.decoder)
        }
        ,dispatchInfo : function (data) {
            //查看消息分发
            layer.open({
                    title : "查看消息分发",
                    area: ['90%', '90%'],
                    offset: 't',
                    fix: false,
                    maxmin: true,
                    shadeClose: false,
                    shade: 0.4,
                    type : 2,
                    content : "/iot/manager/dispatchIndex/"+data.port
                    ,cancel:function (index,layero) {
                        /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                        location.reload();
                    }
            });
        }
        ,edit:function(data){
            layer.open({
                title : "修改端口",
                area: ['90%', '90%'],
                offset: 't',
                fix: false,
                maxmin: true,
                shadeClose: false,
                shade: 0.4,
                type : 2,
                content : "/iot/manager/editPage/"+data.port
                ,cancel:function (index,layero) {
                    /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                    table.reload("iotconfig");
                }
            })
        }
        ,startup:function (data) {
            $.ajax({
                url: "/iot/manager/start/"+data.port,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                            table.reload("iotconfig");
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
        ,shutdown:function (data) {
            $.ajax({
                url: "/iot/manager/shutdown/"+data.port,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                            table.reload("iotconfig");
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
        ,delbatch:function (ports) {
            $.ajax({
                url: "/iot/manager/removeBatch/"+ports,
                type: "get",
                async: false,
                success: function (d) {
                    if (d.code == 200) {
                        layer.alert(d.msg, function () {
                            table.reload("iotconfig");
                        });
                    } else {
                        layer.msg(d.msg, {shift: 6, icon: 2});
                    }
                }, error: function () {
                    layer.msg('请求失败', {icon: 2});
                }
            });
        }
        ,view :function (id) {
            layer.open({
                title : "详情",
                area: ['90%', '90%'],
                offset: 't',
                fix: false,
                maxmin: true,
                shadeClose: false,
                shade: 0.4,
                type : 2,
                content : "/iot/dispatch/view/"+id
                ,cancel:function (index,layero) {
                    /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                    table.reload("iotconfig");
                }
            })
        }
    }

    function openLayer(clspath){
        layer.open({
            title : "上传文件",
            area: ['90%', '90%'],
            offset: 't',
            fix: false,
            maxmin: true,
            shadeClose: false,
            shade: 0.4,
            type : 2,
            content : "/iot/manager/uploadcls?clspath="+clspath
            ,cancel:function (index,layero) {
                /*var dform = $(layero).find("iframe")[0].contentWindow.document;*/
                location.reload();
            }
        })
    };



})
